LiaGui.php

<?php

namespace Tlf\User;

/**
 * Integration class for the liaison gui, currently under development.
 */
class LiaGui {


    protected \Lia $lia;
    protected \Lia\Gui\Package $gui_package;

    public function __construct(\Lia\Gui\Package $gui_package){
        $this->gui_package = $gui_package;
        $this->lia = $gui_package->lia;


$this->lia->view('gui:panel/themes');
        $gui_package->add_panel('lia:user.panel/configure',
            function(){
                return $this->lia->view('user:panel/configure');
            }
        );

        $gui_package->add_panel('lia:user.panel/users',
            function(){
                return $this->lia->view('user:panel/users');
            }
        );
    }

    /**
     * Get an array of available panels
     * @return array<string PanelName, string PanelIdentifier>
     */
    public function get_panel_list(): array {
        return [
            'User App Setup' => 'lia:user.panel/configure',
            'User Management' => 'lia:user.panel/users',
        ];
    }

    /**
     *
     * Setup Liaison User library, during Lia\Gui setup
     *
     * @param $package_dir the directory of the package being setup. Package typically refers to the root dir of a git repo.
     * @param $lia Liaison instance to setup with
     */
    public function setup_liaison(string $package_dir, \Lia $lia){


        $configs = $_POST;

        $dir = dirname(__DIR__);
        $file = $dir.'/gui-config.json';
        if (file_exists($file)){
            $config = json_decode(file_get_contents($file),true);
        } else {
            /** @warning temporary placeholder value, needs replaced with real config loading */
            $config = [
                'package_name' => 'user',
                'base_url' => '/user/',
                'site_url' => 'https://example.com',
                'help_email' => 'example_help@example.com'
            ];
        }


        // get

        // set the base url for the user pages  
        $lia->set('user.base_url', $config['base_url']);  
        // init the user server  
        $user_package = new \Lia\Package\Server($lia, $config['package_name'], $package_dir.'/code/');    

        // load db settings from env file & make pdo  
        if (isset($config['db_name']) && isset($config['db_user']) && isset($config['db_password'])){

            $pdo = new \PDO("mysql:dbname=".$config['db_name'], $config['db_user'], $config['db_password']);  

            $lib = new \Tlf\User\Lib($pdo);  
            $lib->config = [  
                'web_address'=>$config['site_url'],  
                'email_from'=>$config['help_email'],
            ];  


            // uncomment this line, run once, then re-comment this line  
            // $lib->init_db();


            // log the user in  
            try {
                $current_user = $lib->user_from_cookie();  
            } catch (\Exception $e){
                $current_user = false;
            }
            // if there was no cookie, we'll use an unregistered user  
            if ($current_user===false)$current_user = new \Tlf\User($pdo);  
              
            // passes the user & library to user pages (login, register, etc)  
            $user_package->public_file_params = [  
                'user'=>$current_user,  
                'lib'=>$lib  
            ];  
        }

    }

}